package com.rjhch.tonglink.client.base;

import com.tongtech.tlq.base.*;

import java.util.*;
import java.io.*;

public class GetMsgCli {
    static int MyMsgCount = 0;
    private String myQcuName;
    private String myQueName;
    private int myWaitInterval;
    private int reConnCount;
    private int sleepTime;
    private TlqConnection tlqConnection = null;
    private TlqConnContext tlqConnContext = null;
    private TlqSSLContext tlqSSLContext = null;
    private TlqQCU tlqQcu = null;

    public GetMsgCli(String QcuName, String QueName, int WaitInterval) throws
            TlqException {
        myQcuName = QcuName;
        myQueName = QueName;
        myWaitInterval = WaitInterval;
        tlqConnContext = new TlqConnContext();
        tlqConnContext.BrokerId = 111; //根据 tlqcli.conf中的配置

        /*tlqConnContext.BrokerId = -1;
        tlqConnContext.FilesDir= "D:\\TLQCli8\\files";
        tlqConnContext.HostName = "168.1.90.33";
        tlqConnContext.ListenPort = 10261;
 	
		reConnCount = 200;
		sleepTime = 3000;
		
		tlqSSLContext = new TlqSSLContext();
        tlqSSLContext.SSLCAFileName = "E:\\src\\D_TLQ8.1_ClientSSL\\SSL\\secssl\\certs\\cacert.cer";
        tlqSSLContext.SSLCertFileName = "E:\\src\\D_TLQ8.1_ClientSSL\\SSL\\secssl\\certs\\CLIENT.p12";
        tlqSSLContext.SSLCertPwd = "123456";
        tlqSSLContext.SSLFlag = 1;
        tlqSSLContext.SSLProtocolVersion = 1;
        tlqSSLContext.SSLCipher = "AES256-SHA";
        
		tlqConnection = new TlqConnection(tlqConnContext, tlqSSLContext);*/
		//tlqConnection = new TlqConnection(tlqConnContext, reConnCount, sleepTime);
        tlqConnection = new TlqConnection(tlqConnContext);
        tlqQcu = tlqConnection.openQCU(myQcuName);
    }

    static public void printMsgInfo(TlqMessage msgInfo) {
        if ((int) msgInfo.MsgType == 1) {
            System.out.println("Received a File Msg");
            System.out.print("msgInfo.MsgId=" + new String(msgInfo.MsgId));
            System.out.println("   msgInfo.MsgSize=" + (int) msgInfo.MsgSize);

        } else {
            System.out.println("Received a Buffer Msg");
            System.out.print("msgInfo.MsgId=" + new String(msgInfo.MsgId));
            System.out.println("   msgInfo.MsgSize=" + (int) msgInfo.MsgSize);
        }
    }

    public void recvMsg() {
        int msgCount = 0;
        try {
            while (true) {
                TlqMessage msgInfo = new TlqMessage();
                TlqMsgOpt msgOpt = new TlqMsgOpt();

                msgOpt.QueName = myQueName;
                msgOpt.WaitInterval = myWaitInterval;
                /*  msgOpt.MatchOption = TlqMsgOpt.TLQMATCH_PRIORITY; //条件接收
                  msgInfo.Priority = 5;*/
                /* msgOpt.AckMode = TlqMsgOpt.TLQACK_USER;*/
                //用户确认模式
                msgOpt.OperateType = TlqMsgOpt.TLQOT_GET;

                tlqQcu.getMessage(msgInfo, msgOpt);
                msgCount = msgCount + 1;
                
                printMsgInfo(msgInfo);
                
                if (msgOpt.AckMode == TlqMsgOpt.TLQACK_USER) {
                    int acktype = TlqMsgOpt.TLQACK_COMMIT;
                    tlqQcu.ackMessage(msgInfo, msgOpt, acktype);
                }

            }

        } catch (TlqException e) {
            e.printStackTrace();

        } finally {
            MyMsgCount = msgCount;
            try {
                tlqQcu.close();
                tlqConnection.close();
            } catch (TlqException e) {
                e.printStackTrace();
            }
        }
        System.out.println("----------GetMsg is over!------------\n");
    }

    public static void main(String[] argv) throws Exception {
        String QcuName;
        String QueName;
        int WaitInterval = 0;

        if (argv.length < 1) {
            System.out.println("--------------请输入参数！--------------\n");
            System.out
                    .println("GetMsgCli QcuName QueName WaitInterval");
            return;
        }
        if (argv.length != 3) {
            System.out.println("---------您输入的参数格式不对，请重新输入！---------");
            System.out
                    .println("GetMsgCli QcuName QueName WaitInterval");
        } else {
            QcuName = argv[0];
            QueName = argv[1];
            WaitInterval = Integer.parseInt(argv[2]);
            System.out.println(
                    "--------------------receive message begin------------------");
            GetMsgCli GM = new GetMsgCli(QcuName, QueName, WaitInterval);
            GM.recvMsg();

        }
        System.out.println("-------共接收消息" + MyMsgCount + "条-------");
    }
}
